package com.remiss.seckilldemo.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.remiss.seckilldemo.pojo.User;
import com.remiss.seckilldemo.vo.ResposeBean;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 自动生成用户
 */
@Slf4j
public class UserUtil {
    private static void cresteUser(int count) throws Exception {
        List<User> list = new ArrayList<>();
        for (int i = 0 ; i < count ; i ++){
            User user = new User();
            Long userid = 13000000001L + i;
            user.setId(""+userid);
            user.setNickname("user" +i);
            user.setPassword("1234567");
            user.setLoginCount(1);
            user.setRegisterDate(new Date());
            list.add(user);
        }
        log.info("开始生成用户");
        Connection connection = getConn();
        String sql = "insert into t_user(login_count,nickname,register_date,password,id) values(?,?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0 ; i < list.size() ; i++){
            User user = list.get(i);
            preparedStatement.setInt(1,user.getLoginCount());
            preparedStatement.setString(2,user.getNickname());
            preparedStatement.setTimestamp(3,new Timestamp(user.getRegisterDate().getTime()));
            preparedStatement.setString(4,user.getPassword());
            preparedStatement.setString(5,user.getId());
            preparedStatement.addBatch();
        }
        preparedStatement.executeBatch();
        preparedStatement.clearParameters();
        connection.close();
        log.info("插入完成");
        String urlStr = "http://localhost:8080/login/doLogin";
        File file = new File("C:\\Users\\94363\\Desktop\\config.txt");
        if(file.exists()){
            file.delete();
        }
        RandomAccessFile raf = new RandomAccessFile(file,"rw");
        file.createNewFile();
        raf.seek(0);
        for (int i = 0 ;i < list.size() ;i ++){
            User user = list.get(i);
            URL url = new URL(urlStr);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream out = httpURLConnection.getOutputStream();
            String params = "mobile=" + user.getId() +"&password=1234567";
            out.write(params.getBytes());
            out.flush();
            InputStream inputStream = httpURLConnection.getInputStream();
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] buff = new byte[1024];
            int len = 0 ;
            while((len = inputStream.read(buff))>=0){
                bout.write(buff,0,len);
            }

            String response = new String(bout.toByteArray());
            inputStream.close();
            bout.close();
            ObjectMapper mapper = new ObjectMapper();
            ResposeBean resposeBean = mapper.readValue(response, ResposeBean.class);
            String userTicket = (String) resposeBean.getObject();
            String row = user.getId() + "," + userTicket;
            raf.seek(raf.length());
            raf.write(row.getBytes());
            raf.write("\r\n".getBytes());
            log.info(user.getId() + ": " + userTicket +": " +response);

        }
        raf.close();


    }
    private static Connection getConn() throws ClassNotFoundException, SQLException {
        String url = "jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "123456";
        String driver = "com.mysql.cj.jdbc.Driver";
        Class.forName(driver);
        return DriverManager.getConnection(url,username,password);
    }

    public static void main(String[] args) {
        try {
            cresteUser(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
